package com.isky.visual.proj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isky.visual.proj.entity.PanelGroupProject;
import com.isky.visual.proj.entity.ProjectShareUser;
import com.isky.visual.proj.mapper.ProjectShareUserMapper;
import com.isky.visual.proj.service.IProjectShareUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wenjs
 */
@Service
public class ProjectShareUserServiceImpl extends ServiceImpl<ProjectShareUserMapper, ProjectShareUser> implements IProjectShareUserService {
    @Override
    public boolean doInsert(ProjectShareUser projectShareUser) {

        QueryWrapper<ProjectShareUser> queryObject= new QueryWrapper<>();
        ProjectShareUser p = new ProjectShareUser();
        p.setUserId(projectShareUser.getUserId());
        p.setProjectId(projectShareUser.getProjectId());
        queryObject.setEntity(p);
        List<ProjectShareUser> projectShareUsers = this.baseMapper.selectList(queryObject);
        if(projectShareUsers != null && !projectShareUsers.isEmpty()){
            int i = 0;
            for(ProjectShareUser pgp : projectShareUsers){
                if (i++ == 0) {
                    if (!pgp.getValid()) {
                        pgp.setValid(true);
                        this.baseMapper.updateById(pgp);
                    }
                    continue;
                }
                this.baseMapper.deleteById(pgp.getId());
            }
            return true;
        }

        return this.save(projectShareUser);
    }

}